unit unitcontrolasistencia;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DbCtrls, sqldb, db, unitmainform;

type

  { TFormControlAsistencia }

  TFormControlAsistencia = class(TForm)
    Button1: TButton;
    Button2: TButton;
    DataSource1: TDataSource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SQLQuery1: TSQLQuery;
    SQLQuery2: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: char);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    tipo : string;
  end;

var
  FormControlAsistencia: TFormControlAsistencia;

implementation

{$R *.lfm}

{ TFormControlAsistencia }

procedure TFormControlAsistencia.FormShow(Sender: TObject);
begin
  Edit1.Enabled := true;
  Edit1.Text := '';
  Edit2.Text := '';
  DBComboBox1.Text := '';
  DBComboBox1.Enabled := false;

  Edit1.SetFocus;
end;

procedure TFormControlAsistencia.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := MainForm.PQConnection1;
  SQLQuery1.Transaction := MainForm.SQLTransaction1;
  SQLQuery2.DataBase := MainForm.PQConnection1;
  SQLQuery2.Transaction := MainForm.SQLTransaction1;
end;

procedure TFormControlAsistencia.Edit1KeyPress(Sender: TObject; var Key: char);
var
  activo : boolean;
begin
  if Key = #13 then
  begin
    if SQLQuery2.Active = true then
      SQLQuery2.Close;

    SQLQuery2.SQL.Text := 'select * from alumno where alm_dni = ' + Edit1.Text;

    SQLQuery2.Open;

    while not SQLQuery2.EOF do
    begin
      Edit2.Text := SQLQuery2.FieldByName('alm_apellido').AsString + ', ' + SQLQuery2.FieldByName('alm_nombre').AsString;
      SQLQuery2.Next;
      activo := strtobool(SQLQuery2.FieldByName('alm_activo').AsString);
    end;
  end;

  SQLQuery2.Close;
  SQLQuery1.Close;

  if activo then
  begin
    if Edit2.Text <> '' then
      begin
        Edit1.Enabled := false;

        if tipo = 'ENTRADA' then
          SQLQuery1.SQL.Text := 'select dpt_nombre from vw_almdpt where alm_dni = ' + Edit1.Text
        else
          SQLQuery1.SQL.Text := 'select dpt_nombre from vw_almdpt where dpt_cdg = '
            + '(select ast_deporte from asistencia where ast_cdg = (select max(ast_cdg) from asistencia where ast_alumno = '
            + Edit1.text + ')) limit 1';

        SQLQuery1.Open;

        DBComboBox1.DataField := 'dpt_nombre';
        DBComboBox1.Clear;

        while not SQLQuery1.EOF do
        begin
          DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
          SQLQuery1.Next;
        end;

        DBComboBox1.Enabled := true;
        DBComboBox1.SetFocus;
      end;
  end
  else
  begin
    ShowMessage('ALUMNO INACTIVO! Regularice su situacion...');
    close();
  end;
end;

procedure TFormControlAsistencia.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active then
    SQLQuery1.Close;
  if SQLQuery2.Active then
    SQLQuery2.Close;
end;

procedure TFormControlAsistencia.Button2Click(Sender: TObject);
begin
  Close();
end;

procedure TFormControlAsistencia.Button1Click(Sender: TObject);
var
  actividad : string;
  dpt_cdg : string;
  QueryString : string;

begin
  actividad := DBComboBox1.Text;

  if SQLQuery2.Active then
    SQLQuery2.Close;

  SQLQuery2.SQL.Text := 'select dpt_cdg from vw_almdpt where dpt_nombre = ''' + actividad + ''' and alm_dni = ' + Edit1.Text;

  SQLQuery2.Open;

  while not SQLQuery2.EOF do
  begin
    dpt_cdg := SQLQuery2.FieldByName('dpt_cdg').AsString;
    SQLQuery2.Next;
  end;

  SQLQuery2.Close;

  QueryString := 'insert into asistencia(ast_alumno, ast_deporte, ast_tipo) values(' + Edit1.Text + ', ' + dpt_cdg
    + ', ''' + tipo + ''')';

  MainForm.PQConnection1.ExecuteDirect('BEGIN WORK;');
  MainForm.PQConnection1.ExecuteDirect(QueryString);
  MainForm.PQConnection1.ExecuteDirect('COMMIT WORK;');

  close();
end;

end.

